﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

using Lenovo.CFI.BLL;
using Lenovo.CFI.BLL.DicMgr;
using Lenovo.CFI.BLL.Sys;
using Lenovo.CFI.Common;
using Lenovo.CFI.Web.Helper;
using Lenovo.CFI.Common.Sys;
using Lenovo.CFI.Common.Dic;

namespace Lenovo.CFI.Web.VP
{
    public partial class Stat3 : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                for (int i = 2012; i < 2020; i++)
                {
                    this.DdlYear.Items.Add(new ListItem(i.ToString(), i.ToString()));
                }
                this.DdlYear.Items.FindByValue(DateTime.Now.Year.ToString()).Selected = true;

                this.GvList.Visible = false;

                this.Page.Form.DefaultButton = this.BtnStat.UniqueID;
            }
        }


        object[] all = null;
        DateTime from;
        DateTime to;
        Topic[] topics;
        protected void BtnStat_Click(object sender, EventArgs e)
        {
            int year = Convert.ToInt32(this.DdlYear.SelectedValue);

            this.from = new DateTime(year, 1, 1);
            this.to = new DateTime(year, 12, 31);

            List<object[]> teamNumbers = new StatBl().TeamNumberByTime(from, to);
            this.topics = (Topic[])teamNumbers[teamNumbers.Count - 1];
            teamNumbers.RemoveAt(teamNumbers.Count - 1);
            this.all = teamNumbers[teamNumbers.Count - 1];
            teamNumbers.RemoveAt(teamNumbers.Count - 1);

            this.GvList.Visible = true;
            this.GvList.DataSource = teamNumbers;
            this.GvList.DataBind();
        }



        protected void GvList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)    // 数据行
            {
                object[] number = (object[])e.Row.DataItem;

                string dateF = this.from.ToString("yyyy-MM-dd");
                string dateT = this.to.ToString("yyyy-MM-dd");

                ((Literal)(e.Row.FindControl("LtrSummary"))).Text = String.Format(@"<a href=""{0}"" target=""_blank"">{1}</a>",
                    UrlHelper.GenerateUrl("search", "topicdf", dateF, "topicdt", dateT, "stat", "1", "teamid", number[0].ToString()), number[3]);

                StringBuilder sb = new StringBuilder();
                for(int i=0;i<topics.Length;i++)
                {
                    sb.AppendFormat(@"{2}<a href=""{0}"" target=""_blank"">{1}</a>{3}",
                        UrlHelper.GenerateUrl("search", "stat", "1", "topicid", topics[i].ID.ToString(), "teamid", number[0].ToString()), number[3 + i],
                        (i == 0) ? "" : @"<td class=""topics"">",
                        (i == topics.Length - 1) ? "" : "</td>");
                }
                ((Literal)(e.Row.FindControl("LtrTopics"))).Text = sb.ToString();

                ((Literal)(e.Row.FindControl("LtrTeam"))).Text = number[1].ToString();

                ((Literal)(e.Row.FindControl("LtrManager"))).Text = number[2].ToString();

            }
            else if (e.Row.RowType == DataControlRowType.Footer)    // Footer
            {
                string dateF = this.from.ToString("yyyy-MM-dd");
                string dateT = this.to.ToString("yyyy-MM-dd");

                ((Literal)(e.Row.FindControl("LtrSummary"))).Text = String.Format(@"<a href=""{0}"" target=""_blank"">{1}</a>",
                    UrlHelper.GenerateUrl("search", "topicdf", dateF, "topicdt", dateT, "stat", "1"), this.all[3]);

                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < topics.Length; i++)
                {
                    sb.AppendFormat(@"{2}<a href=""{0}"" target=""_blank"">{1}</a>{3}",
                        UrlHelper.GenerateUrl("search", "stat", "1", "topicid", topics[i].ID.ToString()), this.all[3 + i],
                        (i == 0) ? "" : @"<td class=""topics"">",
                        (i == topics.Length - 1) ? "" : "</td>");
                }
                ((Literal)(e.Row.FindControl("LtrTopics"))).Text = sb.ToString();
            }
            else if (e.Row.RowType == DataControlRowType.Header)    // Header
            {
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < topics.Length; i++)
                {
                    sb.AppendFormat(@"{1}{0}{2}",
                        topics[i].Title.ToString(),
                        (i == 0) ? "" : @"<th class=""topics"">",
                        (i == topics.Length - 1) ? "" : "</th>");
                }
                ((Literal)(e.Row.FindControl("LtrTopics"))).Text = sb.ToString();
            }
        }
    }
}